#include "sceneEnd.h"
#include "../manager/gameMer.h"
#include "sceneMain.h"

void STG::sceneEnd::init(){
    if(!SDL_IsTextInputActive()){
        SDL_StartTextInput();
        if(!SDL_IsTextInputActive()){
            SDL_Log("SDL_StartTextInput() failed: %s", SDL_GetError());
        }
    }
    bgm = Mix_LoadMUS("../assets/music/03_Racing_Through_Asteroids_Loop.ogg");
    if(bgm == nullptr){
        SDL_Log("Mix_LoadMUS() failed: %s", Mix_GetError());
    }
    Mix_PlayMusic(bgm, -1);
}

void STG::sceneEnd::update(float dt){
    blink += dt;
    if(blink > 1.0f){
        blink -= 1.0f;
    }
}

void STG::sceneEnd::clean(){
    if(bgm != nullptr){
        Mix_HaltMusic();
        Mix_FreeMusic(bgm);
        bgm = nullptr;
    }
}

void STG::sceneEnd::render(){
    //GameMer::instance()->renderTextCenter(std::string("游戏结束"), 0.4, false);
    
    if(isWrite)
    {
        renderText1();
    }
    else{
        renderText2();
    }
}

void STG::sceneEnd::handleEvents(SDL_Event* e){
    if(isWrite){
        std::string temp = name + e->text.text;
        if(e->type == SDL_TEXTINPUT && temp.length() <= 16)
        {
            name = temp;
        }
        if(e->type == SDL_KEYDOWN){
            if(e->key.keysym.scancode == SDL_SCANCODE_RETURN){
                isWrite = false;
                SDL_StopTextInput();
                if(name == ""){
                    name = "no nane";
                }
                GameMer::instance()->insertLeaderBoard(GameMer::instance()->getScore(),name);
                name.clear();
            }else if(e->key.keysym.scancode == SDL_SCANCODE_BACKSPACE){
                removeLastUTF8Char(name);
            }
        }
    }else{
        if(e->type == SDL_KEYDOWN){
            if(e->key.keysym.scancode == SDL_SCANCODE_H){
                GameMer::instance()->saveLeaderBoard();
                auto scene = new STG::sceneMain();
                GameMer::instance()->changeScene(scene);
            }
        }
    }
}

void STG::sceneEnd::renderText1()
{
    std::string score = std::to_string(GameMer::instance()->getScore());
    GameMer::instance()->renderTextCenter(std::string("您的得分是："+score), 0.1f, false);
    GameMer::instance()->renderTextCenter(std::string("游戏结束"), 0.4f, true);
    GameMer::instance()->renderTextCenter(std::string("请输入您的名字按回车键确认："),0.6f, false);
    if(name != ""){
        SDL_Point p = GameMer::instance()->renderTextCenter(name, 0.8f, false);
        if(blink < 0.5f){
            GameMer::instance()->renderText(std::string("_"), p.x, p.y,false);
        }
    }else{
        if(blink < 0.5f){
            GameMer::instance()->renderTextCenter(std::string("_"),0.8f, false);
        }
    }
}

void STG::sceneEnd::renderText2()
{
    std::string title = std::string("leaderboard");
    GameMer::instance()->renderTextCenter(title, 0.1f, true);
    int j = 1;
    int y = GameMer::instance()->getHeight() * 0.2f;
    for(auto& i : GameMer::instance()->getLeaderBoard()){
        std::string str = std::to_string(j) + "." + i.second;
        std::string str2 = std::to_string(i.first);
        GameMer::instance()->renderText(str, 100, y ,false);
        GameMer::instance()->renderText(str2, 100, y );
        y += 50;
        j++;
    }
    if(blink < 0.5f)
        GameMer::instance()->renderTextCenter("press H to return to the main", 0.8f);
}

void STG::sceneEnd::removeLastUTF8Char(std::string& str){
    if(str.empty()) return;
    auto lastchar = str.back();
    if ((lastchar & 0b10000000) == 0b10000000) {//中文UTF-8编码格式下三个字节，检查是否是后续字节
        str.pop_back();
        while ((str.back() & 0b11000000) != 0b11000000) //判断是否是中文的第一个字节
        {
            str.pop_back();
        }
    }
    str.pop_back();
}
